package com.vrv.common.view;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils.TruncateAt;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TableLayout;
import android.widget.TableLayout.LayoutParams;
import android.widget.TableRow;
import android.widget.TextView;

import com.vrv.app.R;
import com.vrv.common.utils.StringUtils;
import com.vrv.common.utils.ViewUtils;

/**
 *
 * @author 曹保利
 * @version 1.0.0
 */
public class CommonListAdapter extends BaseAdapter {

    // 第1列的最大宽度
    protected int column1MaxWidth;

    private String[] listKeys;

    private Context context = null;

    private List<Map<String, Object>> dataSource = new ArrayList<Map<String, Object>>();

    public CommonListAdapter(Context context, List<Map<String, Object>> data, String[] listKeys) {
        super();
        this.dataSource = data;
        this.context = context;
        this.listKeys = listKeys;
    }

    @Override
    public int getCount() {
        return dataSource.size();
    }

    @Override
    public Object getItem(int position) {
        return (Map<String, Object>) dataSource.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {

        TableLayout tableLayout = null;
        view = LayoutInflater.from(context).inflate(R.layout.common_list_item, null);
        tableLayout = (TableLayout) view;

        // 隔行换色
        if (position % 2 == 0) {
            view.setBackgroundResource(R.drawable.list_item_odd_state);
        } else {
            view.setBackgroundResource(R.drawable.list_item_even_state);
        }

        TableRow row = null;
        TextView labelText = null;
        TextView valueText = null;

        if (position == 0) {
            initColumn1MaxWidth(listKeys.length);
        }
        tableLayout.setColumnShrinkable(1, true);

        for (int i = 0; i < listKeys.length; i++) {
            String key = String.valueOf(listKeys[i]);
            String label = ViewUtils.getStringByKey(context, key);
            String value = String.valueOf(dataSource.get(position).get(key));
            if (StringUtils.isNotBlank(label)) {

                labelText = getTextViewLabel();
                valueText = getTextViewValue();
                labelText.setText(label + ":");
                valueText.setText(value);

                row = new TableRow(context);
                row.setHorizontalGravity(Gravity.LEFT);
                row.addView(labelText);
                row.addView(valueText);

                // if (Constants.DEBUG)Log.v(Constants.TAG, "{itemIndex:" +
                // position + ", label:" + label + ", value:" + value + "}");

                tableLayout.addView(row);

                if (listKeys.length > 1 && i < listKeys.length - 1) {
                    tableLayout.addView(getSegmentLine(), getLayoutParams());
                }
            }
        }

        return tableLayout;
    }

    public void clear() {
        dataSource.clear();
    }

    /**
     * 列名Label
     */
    private TextView getTextViewLabel() {
        final TextView text = new TextView(context);
        text.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16L);
        text.setTextColor(Color.BLACK);
        text.setGravity(Gravity.RIGHT);
        text.setWidth(column1MaxWidth);
        text.setMinWidth(50);
        text.getPaint().setFakeBoldText(true);

        return text;
    }

    /**
     * 列的值
     */
    private TextView getTextViewValue() {
        final TextView text = new TextView(context);
        text.setTextColor(Color.BLACK);
        text.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16L);
        text.setEllipsize(TruncateAt.END);
        return text;
    }

    /**
     * 分割线
     *
     * @return
     */
    private TextView getSegmentLine() {
        TextView text = new TextView(context);
        text.setBackgroundColor(Color.TRANSPARENT);
        text.setHeight(ViewUtils.dip2px(context, 1));
        text.setBackgroundColor(context.getResources().getColor(R.color.splite_line_color));
        return text;
    }

    /**
     * 获取布局参数
     *
     * @return
     */
    private LayoutParams getLayoutParams() {
        return getLayoutParams(1);
    }

    /**
     * 获取布局参数
     *
     * @param height
     *            高度
     * @return
     */
    @SuppressWarnings("deprecation")
    private LayoutParams getLayoutParams(int height) {
        return new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, height);
    }

    /**
     * 获取标签列最大的宽度
     *
     * @param count
     * @param length
     */
    private void initColumn1MaxWidth(int length) {
        for (int i = 0; i < length; i++) {
            String key = String.valueOf(listKeys[i]);
            String label = ViewUtils.getStringByKey(context, key);
            column1MaxWidth = Math.max(column1MaxWidth, ViewUtils.getStringWidthPixel(label, (Activity) context));
        }
    }

}
